-- UIDungeonGamble
-- Created by chengb Apr/15/2015
-- 赌桌界面

UIDungeonGamble = class("UIDungeonGamble", function()
    return cc.Layer:create();
end);

function UIDungeonGamble.create(pos)
    return UIDungeonGamble.new(pos);
end

-- 赌注数量
local MAX_WAGER_NUM = 2;

function UIDungeonGamble:ctor(pos)
    -- 初始化
    self:setName("UIDungeonGamble");
    local node = cc.CSLoader:createNode("layout/dungeon/DungeonGamble.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.pos = pos;

    -- 获取赌注
    local wagers = DungeonTentM.getGambleWagerValue();

    assert(#wagers == 2, "必须配置2个赌注");

    self.wagers = wagers;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIDungeonGamble:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIDungeonGamble:registerEventCallback()
    -- 关注赌博事件
    EventMgr.register("UIDungeonGamble", event.DO_GAMBLE, function(args)
        local result = args.result;
        if result == true then
            -- 赢了
            local bonus = args.bonus;
            local msg = string.format(getLocStr("gamble_win"), bonus[3]);
            alert(msg);
        else
            -- 输了
            local wagerValue = args.wagerValue;
            local msg = string.format(getLocStr("gamble_loss"), wagerValue);
            alert(msg);
        end

        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIDungeonGamble");
    end)

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDungeonGamble 界面析构清理");

            EventMgr.removeAll("UIDungeonGamble");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIDungeonGamble:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIDungeonGamble");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册押注按钮点击事件
    for i = 1, MAX_WAGER_NUM do
        local btnWager = findChildByName(node, "CT/btn_wager" .. i);
        local function onClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_click");
                local wager = sender.wager;
                if ME.user:queryAttrib("search_point") < wager then
                    -- 探索点不足
                    local name = FieldsM.getFieldName("search_point");
                    alert(string.format(getLocStr("something_not_enough"), name));
                    return;
                end

                -- 赌博
                DungeonActionM.go("do_gamble", self.pos, wager);
            end
        end
        btnWager.wager = self.wagers[i];
        btnWager:addTouchEventListener(onClick);
        TextStyleM.setTextStyle(btnWager, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btnWager:setTitleText(string.format(getLocStr("wager_format"), btnWager.wager));
    end
end

-- 重绘
function UIDungeonGamble:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(descLabel, getLocStr("gamble_dialog"));
    titleLabel:setString(getLocStr("gambling_table"));
    iconImg:loadTexture(getLevelItemIconPath("gamble"));

    iconImg:setScale(1.5);
    TextStyleM.setArtWordTitleStyle(titleLabel);
end

